package cn.shop.dys.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;

import com.lidroid.xutils.view.annotation.ViewInject;

import cn.shop.dys.R;
import cn.shop.dys.bean.UserInfo;
import cn.shop.dys.constant.SPConstant;
import cn.shop.dys.util.BusFactory;
import cn.shop.dys.util.SPUtil;
import cn.shop.dys.util.UserInfoDBUtil;
import cn.shop.dys.widget.BtnImgEditText;
import cn.shop.dys.widget.ButtonEditText;
import cn.shop.lib_common.CommonUtil;


/**
 * 用户昵称
 * Created by jian on 2016/4/19.
 */
public class UserDetailsNameFragment extends BaseFragment{
    @ViewInject(R.id.bet_edit_name)
    private BtnImgEditText mEditName;
    private Context mContext;
    @ViewInject(R.id.btn_save)
    private Button mSave;
    @Override
    public View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_user_details_name,null);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mContext = getActivity();
        init();
    }

    private void init() {
        mSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = mEditName.getText().toString().trim();
                if(TextUtils.isEmpty(name)){
                    mEditName.setStatus(BtnImgEditText.STATUS_ERROR);
                    CommonUtil.showToast(mContext,"请输入昵称");
                    return;
                }

                UserInfo userInfo = UserInfoDBUtil.queryUserInfoByUserId(SPUtil.getString(mContext, SPConstant.USER_INFO_USER_ID));
                userInfo.setNickname(name);
                BusFactory.getBus().post(userInfo);
                UserInfoDBUtil.updateUserInfo(userInfo);
                CommonUtil.showToast(mContext,"保存成功");
                View view =getActivity().getWindow().peekDecorView();
                if (view != null) {
                    InputMethodManager inputmanger = (InputMethodManager)mContext. getSystemService(Context.INPUT_METHOD_SERVICE);
                    inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0);
                }
                onBackPressed();
            }
        });
    }
}
